package com.shilan.administrator.qintfm.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.shilan.administrator.qintfm.R;
import com.shilan.administrator.qintfm.Utils.MediaUtils;
import com.shilan.administrator.qintfm.beans.SongInfo;
import com.shilan.administrator.qintfm.fragment.MusicLIstFragment;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/1/14.
 */
public class MusicListFragmentAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<SongInfo> songInfos;

    public MusicListFragmentAdapter(Context context, ArrayList<SongInfo> songInfos) {
        this.context = context;
        this.songInfos = songInfos;
        Log.i("qwqw",songInfos.toString());
    }

    public void setSongInfos(ArrayList<SongInfo> songInfos) {
        this.songInfos = songInfos;
    }

    @Override
    public int getCount() {
        return songInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return songInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder ;
        if (convertView ==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_musiclist_fragment,null);
            viewHolder = new ViewHolder();
            viewHolder.tv_songer = (TextView) convertView.findViewById(R.id.tv_songer_item_music);
            viewHolder.tv_songName = (TextView) convertView.findViewById(R.id.tv_songName_item_music);
            viewHolder.tv_time = (TextView) convertView.findViewById(R.id.tv_time_item_music);
            viewHolder.iv_picture = (ImageView) convertView.findViewById(R.id.iv_item_picture_music);
            convertView.setTag(viewHolder);

        }else {
            viewHolder = (ViewHolder) convertView.getTag();

        }
        SongInfo songInfo =songInfos.get(position);
        viewHolder.tv_songName.setText(songInfo.getTitle());
        viewHolder.tv_songer.setText(songInfo.getArtist());
        //获取歌曲时间长度
        viewHolder.tv_time.setText(MediaUtils.formatTime(songInfo.getDuration()));


        return convertView;
    }

    static class ViewHolder{
        TextView tv_songName;
        TextView tv_songer;
        TextView tv_time;
        ImageView iv_picture;
    }
}
